#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

const int MOD = 100000007;

// 定义操作类型的枚举
enum class OperationType {
    ADD_PLUS,
    ADD_MULTIPLY,
    MODIFY_PLUS,
    MODIFY_MULTIPLY,
    DELETE,
    QUERY
};

// 解析输入的操作
OperationType parseOperation(const std::string &opStr) {
    if ( opStr == "add" ) return OperationType::ADD_PLUS;
    if ( opStr == "modify" ) return OperationType::MODIFY_PLUS;
    if ( opStr == "delete" ) return OperationType::DELETE;
    if ( opStr == "query" ) return OperationType::QUERY;
    return OperationType::ADD_PLUS;
}

// 计算当前公式的值
int calculate(const std::vector<std::pair<char, int>> &formula) {
    int result = 1;
    std::vector<int> temp;
    temp.push_back(result);

    for (const auto &term: formula) {
        if ( term.first == '+' ) {
            temp.push_back(term.second);
        } else if ( term.first == '*' ) {
            temp.back() = (1LL * temp.back() * term.second) % MOD;
        }
    }

    result = 0;
    for (int num: temp) {
        result = (result + num) % MOD;
    }

    return result;
}

int main() {
    int T;
    std::cin >> T;

    for (int t = 1; t <= T; ++t) {
        int N;
        std::cin >> N;
        std::vector<std::pair<char, int>> formula;  // 存储公式的每一项，pair 第一个元素为符号，第二个元素为数值

        std::cout << "Case " << t << ":" << std::endl;

        for (int i = 0; i < N; ++i) {
            std::string op;
            std::cin >> op;
            OperationType opType = parseOperation(op);

            if ( opType == OperationType::ADD_PLUS ) {
                std::string symbol;
                int A;
                std::cin >> symbol >> A;
                formula.emplace_back(symbol[0], A);
            } else if ( opType == OperationType::ADD_MULTIPLY ) {
                std::string symbol;
                int A;
                std::cin >> symbol >> A;
                formula.emplace_back(symbol[0], A);
            } else if ( opType == OperationType::MODIFY_PLUS ) {
                int B;
                std::string symbol;
                int A;
                std::cin >> B >> symbol >> A;
                formula[B - 2] = {symbol[0], A};
            } else if ( opType == OperationType::MODIFY_MULTIPLY ) {
                int B;
                std::string symbol;
                int A;
                std::cin >> B >> symbol >> A;
                formula[B - 2] = {symbol[0], A};
            } else if ( opType == OperationType::DELETE ) {
                int B;
                std::cin >> B;
                formula.erase(formula.begin() + B - 2);
            } else if ( opType == OperationType::QUERY ) {
                int result = calculate(formula);
                std::cout << result << std::endl;
            }
        }
    }

    return 0;
}